﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;


namespace AutoSubmit.Lib
{
    public class ArticleHelper
    {
        public IDictionary<string, string> configs;
        public ArticleHelper(string path)
        {
            configs = new Dictionary<string, string>();
            using (StreamReader sr = File.OpenText(path))
            {
                string next = string.Empty;
                while ((next = sr.ReadLine()) != null)
                {
                    if (next.Trim().Length == 0 || next.Trim().StartsWith("#"))
                    {
                        continue;
                    }

                    string[] arrs = next.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
                    string value = arrs[1];
                    if (arrs.Length > 2)
                    {
                        for (int i = 2; i < arrs.Length; i++)
                        {
                            value += "=" + arrs[i];
                        }
                    }

                    // if it's article body
                    if (arrs[0].Equals("submit.body"))
                    {
                        while (!(next = sr.ReadLine()).Contains("-----[[[END_OF_ARTICLE_BODY]]]-----"))
                        {
                            value = value + "\n" + next;
                        }
                    }

                    
                    configs.Add(arrs[0], value);
                }
 
            }
        }
        public string GetValueByKey(string key)
        {
            if (configs.ContainsKey(key)) return configs[key];
            return string.Empty;
        }

        public bool ContainsKey(string key)
        {
            return configs.ContainsKey(key);
        }
    }
}
